package cn.edu.hhtc.stsystem.service.utils;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.util.EncryptionUtils;

/**
 * service层工具类.
 * 
 * @author linyueqin
 * 
 */
@SuppressWarnings("deprecation")
public class ServiceUtil {

	private static final String ENCRYPT_KEY = "0123456789&abcdefghijklmnopqrstuvwxyz";

	/**
	 * 加密.
	 * 
	 * @param str
	 * @return
	 */
	public static String encodeToDes3(String str) {

		return ServiceUtil.encodeToDes3(str, ENCRYPT_KEY);
	}

	/**
	 * 解密.
	 * 
	 * @param str
	 * @return
	 */
	public static String decodeFromDes3(String str) {

		return ServiceUtil.decodeFromDes3(str, ENCRYPT_KEY);
	}

	public static String encodeToDes3(String str, String encryptKey) {

		try {
			if (StringUtils.isBlank(str))
				return null;
			return java.net.URLEncoder.encode(EncryptionUtils.encrypt(encryptKey, str), "utf-8");
		} catch (Exception e) {
			return null;
		}
	}

	public static String decodeFromDes3(String str, String encryptKey) {

		try {
			if (StringUtils.isBlank(str))
				return null;
			String tmp = null;
			tmp = str.replace("+", "%2B");
			tmp = tmp.replace("=", "%3D");
			tmp = tmp.replace("%25", "%");
			return EncryptionUtils.decrypt(encryptKey, java.net.URLDecoder.decode(tmp, "utf-8"));
		} catch (Exception e) {
			return null;
		}
	}
}
